// subpackagesA/pages/orderManagementZR/orderManagementZR.ts
import {
  ZRorderNums,
  ZRCanSeizeOrders,
  ZRPending,
  ZRCompleted,
  ZRGrabbing,
} from "../../../api/order";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    menu: [
      {
        title: "可抢单",
        bgColor: "#FF6B6B",
        fontColor: "#FF6B6B",
        num: 0,
        active: 1,
      },
      {
        title: "待处理",
        bgColor: "#FFC001",
        fontColor: "#FFC001",
        num: 0,
        active: 2,
      },
      {
        title: "已完成",
        bgColor: "#5194F8",
        fontColor: "#5194F8",
        num: 0,
        active: 3,
      },
    ],
    menuItem: [
      {
        title: "今日工单",
        num: 0,
        index: 1,
        bgColor: "#5194F8",
        fontColor: "white",
      },
      {
        title: "历史工单",
        num: 0,
        index: 2,
        bgColor: "#5194F8",
        fontColor: "white",
      },
      {
        title: "催单",
        num: 0,
        index: 3,
        bgColor: "#5194F8",
        fontColor: "white",
      },
      {
        title: "转派工单",
        num: 0,
        index: 4,
        bgColor: "#5194F8",
        fontColor: "white",
      },
    ],
    menuItems: [
      {
        title: "今日工单",
        num: 0,
        index: 1,
        bgColor: "#5194F8",
        fontColor: "white",
      },
      {
        title: "历史工单",
        num: 0,
        index: 2,
        bgColor: "#5194F8",
        fontColor: "white",
      },
    ],
    active: 1,
    menuIndex: 1,
    menusIndex: 1,
    comID: 0,
    userID: 0,
    data: [],
    isFresh: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // this.getQuantity();
    // this.getPending();
    // this.fresh();
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    });
  },
  getQuantity() {
    const comID = wx.getStorageSync("userInfo").data.comID;
    const userID = wx.getStorageSync("userInfo").data.userID;
    const active = this.data.active;
    ZRorderNums(comID, userID).then((res: any) => {
      this.setData({
        "menuItems[0].num": res.data.今日已完成,
        "menuItems[1].num": res.data.历史已完成,
        "menuItem[0].num": res.data.今日未完成,
        "menuItem[1].num": res.data.历史未完成,
        "menuItem[2].num": res.data.催单,
        "menuItem[3].num": res.data.转派,
        "menu[1].num":
          res.data.今日未完成 +
          res.data.历史未完成 +
          res.data.催单 +
          res.data.转派,
        "menu[2].num": res.data.今日已完成 + res.data.历史已完成,
      });
    });
    ZRCanSeizeOrders(comID).then((res: any) => {
      if(active==1){
        this.setData({
          "menu[0].num": res.data.data.length,
          data: res.data.data
        });
      }else{
        this.setData({
          "menu[0].num": res.data.data.length,
          // data: res.data.data
        });
      }

    });
  },
  onMenu(e: any) {
    const comID = wx.getStorageSync("userInfo").data.comID;
    this.setData({
      active: e.currentTarget.dataset.item,
    });
    switch (e.currentTarget.dataset.item) {
      case 1:
        ZRCanSeizeOrders(comID).then((res: any) => {
          this.setData({
            "menu[0].sum": res.data.length,
            data: res.data.data,
          });
        });
        break;
      case 2:
        this.getPending();
        break;
      case 3:
        this.getCompleted();
        break;
    }
  },
  onMenus(e: any) {
    this.setData({
      menuIndex: e.currentTarget.dataset.index,
    });
    this.getPending();
  },
  onMenusItem(e: any) {
    this.setData({
      menusIndex: e.currentTarget.dataset.index,
    });
    this.getCompleted();
  },
  getPending() {
    const comID = this.data.comID;
    const userID = this.data.userID;
    const menuIndex = this.data.menuIndex;
    const active = this.data.active
    console.log('menuIndex:' + menuIndex)
    if(active==2){
      if (menuIndex == 1) {
        ZRPending(comID, 0, userID).then((res: any) => {
          this.setData({
            data: res.data.data,
            isFresh: false,
          });
        });
      } else if (menuIndex == 2) {
        ZRPending(comID, 1, userID).then((res: any) => {
          this.setData({
            data: res.data.data,
            isFresh: false,
          });
        });
      } else if (menuIndex == 3) {
        ZRPending(comID, 2, userID).then((res: any) => {
          this.setData({
            data: res.data.data,
            isFresh: false,
          });
        });
      } else {
        ZRPending(comID, 3, userID).then((res: any) => {
          this.setData({
            data: res.data.data,
            isFresh: false,
          });
        });
      }
    }
  },
  getCompleted() {
    const comID = this.data.comID;
    const userID = this.data.userID;
    const menusIndex = this.data.menusIndex;
    if (menusIndex == 1) {
      ZRCompleted(comID, 0, userID).then((res: any) => {
        this.setData({
          data: res.data.data,
          isFresh: false,
        });
      });
    } else {
      ZRCompleted(comID, 1, userID).then((res: any) => {
        this.setData({
          data: res.data.data,
          isFresh: false,
        });
      });
    }
  },
  fresh() {
    const active = this.data.active;
    const comID = this.data.comID;
    console.log('active:'+ active)
    if (active == 1) {
      ZRCanSeizeOrders(comID).then((res: any) => {
        this.setData({
          "menu[0].sum": res.data.length,
          data: res.data.data,
          isFresh: false,
        });
      });
      this.getQuantity();
    } else if (active == 2) {
      this.getPending();
      this.getQuantity();
    } else {
      this.getCompleted();
      this.getQuantity();
    }
  },
  onGrabbing(e: any) {
    const name = wx.getStorageSync("userInfo").data.name;
    const id = e.currentTarget.dataset.id;
    const comID = this.data.comID;
    const userid = this.data.userID;
    ZRGrabbing(userid, id, comID, name);
    ZRCanSeizeOrders(comID).then((res: any) => {
      this.setData({
        "menu[0].sum": res.data.length,
        data: res.data.data,
      });
    });
  },
  onDetail(e: any) {
    const id = e.currentTarget.dataset.id;
    const active = this.data.active;

    if (active == 2) {
      wx.navigateTo({
        url: "/subpackagesA/pages/repairdetails/repairdetails?id=" + id,
      });
    } else if (active == 3) {
      wx.navigateTo({
        url: "/subpackagesA/pages/processingdetails/processingdetails?id=" + id,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      comID: wx.getStorageSync("userInfo").data.comID,
      userID: wx.getStorageSync("userInfo").data.userID,
    });
    this.getQuantity();
    // this.getPending();
    // this.fresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
